package com.yserp.project.erp.basic.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yserp.common.constant.Constants;
import com.yserp.common.utils.mybatis.MybatisPlusUtil;
import com.yserp.common.utils.text.Convert;
import com.yserp.project.erp.basic.customer.domain.BasisCustomer;
import com.yserp.project.erp.basic.customer.mapper.BasisCustomerMapper;
import com.yserp.project.erp.basic.customer.service.IBasisCustomerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 客户资料Service业务层处理
 *
 * @author yserp
 * @date 2022-06-29
 */
@Service
public class BasisCustomerServiceImpl extends ServiceImpl<BasisCustomerMapper, BasisCustomer> implements IBasisCustomerService {
    @Resource
    private BasisCustomerMapper basisCustomerMapper;

    /**
     * 查询客户资料
     *
     * @param id 客户资料主键
     * @return 客户资料
     */
    @Override
    public BasisCustomer selectBasisCustomerById(Long id) {
        return basisCustomerMapper.selectById(id);
    }

    /**
     * 查询客户资料列表
     *
     * @param basisCustomer 客户资料
     * @return 客户资料
     */
    @Override
    public List<BasisCustomer> selectBasisCustomerList(BasisCustomer basisCustomer) {
        QueryWrapper<BasisCustomer> queryWrapper = new QueryWrapper<>();
        MybatisPlusUtil.notNullField(basisCustomer, queryWrapper);
        return basisCustomerMapper.selectList(queryWrapper);
    }

    /**
     * 新增客户资料
     *
     * @param basisCustomer 客户资料
     * @return 结果
     */
    @Override
    public int insertBasisCustomer(BasisCustomer basisCustomer) {

        return basisCustomerMapper.insert(basisCustomer);
    }

    /**
     * 修改客户资料
     *
     * @param basisCustomer 客户资料
     * @return 结果
     */
    @Override
    public int updateBasisCustomer(BasisCustomer basisCustomer) {
        return basisCustomerMapper.updateById(basisCustomer);
    }

    /**
     * 批量删除客户资料
     *
     * @param ids 需要删除的客户资料主键
     * @return 结果
     */
    @Override
    public int deleteBasisCustomerByIds(String ids) {
        return basisCustomerMapper.deleteBatchIds(Arrays.asList(Convert.toLongArray(ids)));
    }

    /**
     * 删除客户资料信息
     *
     * @param id 客户资料主键
     * @return 结果
     */
    @Override
    public int deleteBasisCustomerById(Long id) {
        return basisCustomerMapper.deleteById(id);
    }

    @Override
    public String checkCustomerCodeUnique(String customerCode) {
        String flag = Constants.NAME_UNIQUE;
        int i = basisCustomerMapper.selectCount(new QueryWrapper<BasisCustomer>().eq("customer_code", customerCode));
        if (i > 0) {
            flag = Constants.NAME_NOT_UNIQUE;
        }
        return flag;
    }
}
